﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace MobileManager
{
    public partial class LogsManager : System.Web.UI.Page
    {
        private MM.Model.T_Users userModel;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["CurUser"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
            else
            {
                userModel = (MM.Model.T_Users)Session["CurUser"];
                if (userModel.RoleID.Equals("3"))
                    return;
            }
            if (!IsPostBack)
            {
                GVDataBind();
                if (userModel.RoleID.Equals("1"))
                {
                    divDelete.Visible = true;
                    inputDateFrom.Value = DateTime.Now.AddMonths(-5).ToString("yyyy-MM-dd");
                    inputDateTo.Value = DateTime.Now.AddMonths(-4).ToString("yyyy-MM-dd");
                    string strAllcount = "0";
                    object o = MM.DBUtility.DbHelperSql.GetSingle("SELECT COUNT(*) FROM T_SysLogs WITH(NOLOCK)");
                    if (o != null)
                        strAllcount = o.ToString();
                    lbAllCount.Text = "当前总记录数：" + strAllcount;
                }
                else divDelete.Visible = false;
            }
        }

        private void GVDataBind()
        {
            string strSQL = string.Format("SELECT * FROM T_SysLogs WITH(NOLOCK) WHERE LogType=N'{0}'", ddlLogs.SelectedValue);
            if (!string.IsNullOrEmpty(txtUserID.Text.Trim()))
                strSQL += string.Format(" AND UserID like '%{0}%'", txtUserID.Text.Trim());
            if (!string.IsNullOrEmpty(txtContent.Text.Trim()))
                strSQL += string.Format(" AND Content like '%{0}%'", txtContent.Text.Trim());
            strSQL += " ORDER BY CreateTime DESC";
            DataSet ds = MM.DBUtility.DbHelperSql.Query(strSQL);
            GVinfo.DataSource = ds;
            GVinfo.DataBind();
            lbCount.Text = "记录数：" + ds.Tables[0].Rows.Count;
        }

        protected void GVinfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVinfo.PageIndex = e.NewPageIndex;
            GVDataBind();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                GVDataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                string strTimeFrom = inputDateFrom.Value;
                string strTimeTo = inputDateTo.Value;
                if (string.IsNullOrEmpty(strTimeFrom) || string.IsNullOrEmpty(strTimeTo))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "开始日期和截止日期不能为空！");
                    return;
                }
                if (Convert.ToDateTime(strTimeFrom) > Convert.ToDateTime(strTimeTo))
                {
                    MM.Common.Common.PageStartAlert(this.Page, "开始日期不能大于截止日期！");
                    return;
                }
                string strSQL = string.Format("DELETE FROM T_SysLogs WHERE (CreateTime BETWEEN '{0}' AND '{1}')", Convert.ToDateTime(strTimeFrom).ToString(), Convert.ToDateTime(strTimeTo).AddDays(1).ToString());
                if (!ddlDelLogs.SelectedValue.Equals("all"))
                    strSQL += string.Format(" AND LogType='{0}'", ddlDelLogs.SelectedValue);
                MM.DBUtility.DbHelperSql.ExecuteSql(strSQL);
                GVDataBind();
                MM.Common.Common.PageStartAlert(this.Page, "删除成功！");
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

    }
}
